// 配置参数
String packageName = "com.easipass.ehub.biz"
String poName = "EhubBizInvitationRlsPO"
// 指定绝对路径
String targetDir = "D:/a-project/ehub/portal-components/ehub/ehub-biz/src/main/java/${packageName.replace('.', '/')}"

// 定义要生成的文件模板
def daoTemplate = """\
package ${packageName}.repository;

import ${packageName}.po.${poName};
import com.easipass.plugin.hibernate.repository.EpRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

@Repository
public interface ${poName.replace("PO", "Dao")} extends EpRepository<${poName}, String>, JpaRepository<${poName}, String>, JpaSpecificationExecutor<${poName}> {
}
"""

def domainServiceTemplate = """\
package ${packageName}.service.domain;

import ${packageName}.po.${poName};
import com.easipass.commons.result.dto.RestMultiResult;
import com.easipass.plugin.data.model.Page;

import javax.persistence.Query;
import java.util.List;
import java.util.Map;

public interface ${poName.replace("PO", "DomainService")} {

    ${poName} save(${poName} po);
    RestMultiResult query(Map<String, Object> params);
    RestMultiResult queryAll(Map<String, Object> params);
    ${poName} findById(String id);
    List<${poName}> saveAll(List<${poName}> list);
    Page queryPageByQuery(List<Query> queryList, Integer offset, Integer limit);
    
}
"""

def domainServiceImplTemplate = """\
package ${packageName}.service.domain.impl;

import ${packageName}.po.${poName};
import ${packageName}.repository.${poName.replace("PO", "Dao")};
import ${packageName}.service.domain.${poName.replace("PO", "DomainService")};
import com.easipass.ehub.client.exceptions.DefaultDomainErrorCode;
import com.easipass.commons.result.dto.RestMultiResult;
import com.easipass.commons.result.exception.ResultBaseException;
import com.easipass.plugin.data.model.Page;
import com.easipass.plugin.hibernate.service.impl.EpHibernateBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.Query;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class ${poName.replace("PO", "DomainServiceImpl")} extends EpHibernateBaseService implements ${poName.replace("PO", "DomainService")} {

    @Autowired
    private ${poName.replace("PO", "Dao")} ${poName.replace("PO", "Dao").toLowerCase()};

    @Override
    public ${poName} save(${poName} po) {
        return ${poName.replace("PO", "Dao").toLowerCase()}.save(po);
    }

    @Override
    public RestMultiResult query(Map<String, Object> params) {
        return this.queryBasicPage(${poName}.class, params);
    }

    @Override
    public RestMultiResult queryAll(Map<String, Object> params) {
        params.put("isAll", true);
        return this.queryBasicPage(${poName}.class, params);
    }

    @Override
    public ${poName} findById(String id) {
        Optional<${poName}> po = ${poName.replace("PO", "Dao").toLowerCase()}.findById(id);
        if (!po.isPresent()) {
            throw new ResultBaseException(DefaultDomainErrorCode.ERROR_CODE001);
        }
        return po.get();
    }

    @Override
    public List<${poName}> saveAll(List<${poName}> list) {
        return ${poName.replace("PO", "Dao").toLowerCase()}.saveAll(list);
    }

    @Override
    public Page queryPageByQuery(List<Query> queryList, Integer offset, Integer limit) {
        offset = Optional.ofNullable(offset).orElse(1);
        limit = Optional.ofNullable(limit).orElse(10);
        return ${poName.replace("PO", "Dao").toLowerCase()}.queryPageByQuery(queryList, offset, limit);
    }
}
"""

// 创建目录并生成文件
def generateFile(String dir, String fileName, String content) {
    def file = new File(dir, fileName)
    file.parentFile.mkdirs()
    file.text = content
}

generateFile("${targetDir}/repository", "${poName.replace("PO", "Dao")}.java", daoTemplate)
generateFile("${targetDir}/service/domain", "${poName.replace("PO", "DomainService")}.java", domainServiceTemplate)
generateFile("${targetDir}/service/domain/impl", "${poName.replace("PO", "DomainServiceImpl")}.java", domainServiceImplTemplate)

println "DAO, DomainService, and DomainServiceImpl classes generated successfully!"
